using Consul;
using Consul.AspNetCore;
using Refit;
using ZhaoXi.MSACommerce.AuthService;
using ZhaoXi.MSACommerce.AuthService.Service;
using ZhaoXi.MSACommerce.Consul.ServiceDiscovery;
using ZhaoXi.MSACommerce.Consul.ServiceRegistrantion;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.


// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHttpApi(builder.Configuration);

var serviceCheck = builder.Configuration.GetSection
    ( "ServiceCheck").Get<ServiceCheckConfiguration>();
serviceCheck ??= new ServiceCheckConfiguration();

builder.Services.AddRefitClient<IUserService>()
    .ConfigureHttpClient(client =>
    {
        client.BaseAddress = new Uri("http://localhost:5001"); 
        client.Timeout = TimeSpan.FromSeconds(30);
    });
builder.Services.AddConsul();
builder.Services.AddConsulService(serviceConfiguration=>
{
    serviceConfiguration.ServiceAddress = new Uri(builder.Configuration["urls"] ?? builder.Configuration["applicationUrl"]);
}, serviceCheck);
builder.Services.AddConsulDiscovery();

builder.Services.AddHealthChecks();

builder.Services.AddControllers();
var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseCors("AllowAny");
app.UseHttpsRedirection();
app.UseHealthChecks(serviceCheck.Path);
app.UseAuthorization();

app.MapControllers();

app.Run();
